/*
 * @Author: your name
 * @Date: 2021-07-02 15:03:50
 * @LastEditTime: 2021-07-03 17:45:23
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \Skip\assets\script\block.ts
 */
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Game from "./Game";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Block extends cc.Component {

    // @property(cc.Node)
    // point: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:
    // 节点最初的坐标
    pointPosition: cc.Vec2 = null;
    // 相对锚点的坐标
    position: cc.Vec2 = null
    canTouch: boolean = false;
    blockType: number = 0;
    children: cc.Node[] = [];
    // 是否发生碰撞 如果发生，则添加块逻辑
    isConllision: boolean = false;
    // score分数，主要是为了后期的数据统计 和块跳的过程 如果只是落在当天块，不需要加分
    score: number = 0;


    onLoad() {
        this.canTouch = false
        // this.pointPosition = this.point.getPosition();
        this.children = this.node.children
    }
    init(position: cc.Vec2, blockType: number, isFirst: number) {
        this.score = 0;
        this.isConllision = false;
        this.blockType = blockType;// 当前显示第几个块
        this.position = position;// 存放白点的相对位置
        this.canTouch = false;// 不可点击
        this.pointPosition = this.children[blockType].getChildByName("point").getPosition();// 通过传过来的blockType 获取对应子节点 点 的位置
        let pos_node = cc.v2(position.x - this.pointPosition.x, position.y - this.pointPosition.y); // 节点要摆放的位置
        if (isFirst >= 3) {
            this.node.setPosition(cc.v2(pos_node.x, pos_node.y + 100));
            cc.tween(this.node)
                .to(0.2, { y: pos_node.y - 10 })
                .to(0.3, { y: pos_node.y + 10 })
                .to(0.3, { y: pos_node.y - 3 })
                .to(0.3, { y: pos_node.y })
                .call(() => {
                    // 调用向下移动的动画
                    Game.inst.blockMove()
                })
                .start()

        } else {
            // 让第一个块不得分
            if (isFirst === 1) this.score = 1;
            this.canTouch = true
            this.node.setPosition(cc.v2(pos_node.x, pos_node.y));
        }
        // 展示节点下对应的子块，隐藏其他子块
        this.setBlock()
    }
    setBlock() {
        this.children.forEach((v, index) => {
            v.active = index === this.blockType;
        })
    }

    updatePosition() {
        this.canTouch = true
        let pos_node = this.node.getPosition();
        this.position = cc.v2(pos_node.x + this.pointPosition.x, pos_node.y + this.pointPosition.y);
    }
    start() {
        console.log('start')
    }

    touchBegin(dt) {
        console.log('touchesBegin')
        let block = this.children[this.blockType].getChildByName("block")
        let blockShadow = this.children[this.blockType].getChildByName("block_shadow")
        block.scaleY = block.scaleY - dt / 6
        blockShadow.scaleY = block.scaleY
        if (block.scaleY <= 0.8) {
            block.scaleY = 0.8
            blockShadow.scaleY = 0.8
        } else {
            Game.inst.hero.y = Game.inst.hero.y - dt / 8 * block.height
        }
        Game.inst.hero.scaleY = block.scaleY * 0.95;
        Game.inst.hero.scaleX = Game.inst.hero.scaleX + dt / 10;
        if (Game.inst.hero.scaleX > 1.2) {
            Game.inst.hero.scaleX = 1.2
        }

    }
    touchEnd() {
        this.canTouch = false;
        console.log('touchend')
        Game.inst.hero.scaleX = 1;
        Game.inst.hero.scaleY = 1;
        let block = this.children[this.blockType].getChildByName("block")
        let blockShadow = this.children[this.blockType].getChildByName("block_shadow")
        let scaleY = 1 - block.scaleY
        cc.tween(block)
            .to(scaleY / 5, { scaleY: 1 + scaleY / 2 })
            .to(scaleY / 8, { scaleY: 1 + scaleY / 5 })
            .to(scaleY / 10, { scaleY: 1 })
            .call(() => {

            })
            .start()
        cc.tween(blockShadow)
            .to(scaleY / 5, { scaleY: 1 + scaleY / 2 })
            .to(scaleY / 8, { scaleY: 1 + scaleY / 5 })
            .to(scaleY / 10, { scaleY: 1 })
            .call((e, index) => {

            })
            .start()
    }

    // update (dt) {}
}
